package test;

import entity.BPNN;
import utils.FileUtil;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * jframe画图工具
 */
public class DrawImage extends JFrame {
    private static final String PATH = FileUtil.RESOURCES + "test_imgs";
    private int sx, sy, ex, ey;

    public DrawImage() throws AWTException {
        int x = 400, y = 200;
        setBounds(x, y, 726, 399);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container c = getContentPane();
        c.setLayout(new GridLayout(1, 2, 10, 10));

        JPanel body = new JPanel();
        body.setBorder(BorderFactory.createTitledBorder("绘制数字"));
        body.setBackground(Color.BLACK);

        JPanel jMenu = new JPanel();
        jMenu.setBorder(BorderFactory.createTitledBorder("点击选择"));
        jMenu.setBackground(new Color(255, 229, 204));

        JButton clear = new JButton("重置");
        clear.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                body.repaint();
            }
        });
        JButton save = new JButton("识别数字");
        save.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                File file = savePic(x + 18, y + 50, body.getWidth() - 20, body.getHeight() - 30);
                List<Map.Entry<Integer, Double>> estimate = BPNN.estimate(file)
                        .stream()
                        .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
                        .limit(3)
                        .toList();
                System.err.println(estimate);
            }
        });
        jMenu.add(clear);
        jMenu.add(save);

        body.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                sx = e.getX();
                sy = e.getY();
            }
        });
        body.addMouseMotionListener(new MouseAdapter() {
            public void mouseDragged(MouseEvent e) {
                Graphics g = body.getGraphics();
                ex = e.getX();
                ey = e.getY();
                g.setColor(Color.WHITE);
                ((Graphics2D) g).setStroke(new BasicStroke(20));
                g.drawLine(sx, sy, ex, ey);
                sx = ex;
                sy = ey;
            }
        });

        c.add(body);
        c.add(jMenu);
        setVisible(true);
    }

    private final Robot robot = new Robot();

    private File savePic(int x, int y, int w, int h) {
        try {
            BufferedImage image = robot.createScreenCapture(new Rectangle(x, y, w, h));
            Image scaledInstance = image.getScaledInstance(28, 28, Image.SCALE_SMOOTH);
            BufferedImage outputImage = new BufferedImage(28, 28, BufferedImage.TYPE_INT_RGB);
            outputImage.getGraphics().drawImage(scaledInstance, 0, 0, null);

            File file = new File(PATH + "/" + FileUtil.getNow() + ".jpg");
            ImageIO.write(outputImage, "jpg", file);
            return file;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
